<?php

/**
 *   员工管理表
 */

namespace app\admin\controller;

use app\facade\Basic;
use think\Exception;
use think\Request;
use app\admin\service\Operator as ServiceOperator;

class Operator extends Base
{
    protected $request;
    protected $service;
    protected $validate;

    /**
     * @describe
     * @author: Bobo
     * @date: 2024-09-13 12:41:16
     * @param [type] @required
     * @return Renderable
     */
    public function __construct(Request $request)
    {
        $this->request = $request;
        $this->service = new \app\admin\service\Operator();
    }
    /**
     *  员工列表
     */
    public function index(Request $request)
    {

        $params = $this->request->params;

        /* 验证 */
        // $this->validateData($param, 'Operator.list');


        $pageIndex  = isset($params['pageIndex'])   ? intval($params['pageIndex']) : 1;
        $pageSize   = isset($params['pageSize'])    ? intval($params['pageSize']) : 10;
        $cname   = isset($params['cname'])   ? trim($params['cname']) : null;
        $phone      = isset($params['phone'])       ? trim($params['phone']) : null;
        $email     = isset($params['email'])      ? trim($params['email']) : null;
        $department     = isset($params['department'])      ? intval($params['department']) : 0;

        $where[] = ['is_del', '=', 0];
        if ($cname) $where[] = ['cname', 'like', '%' . $cname . '%'];
        if ($phone) $where[] = ['phone', 'like', '%' . $phone . '%'];
        if ($email) $where[] = ['email', 'like', '%' . $email . '%'];
        if ($department > 0) $where[] = ['department', '=', $department];

        $result = $this->service->getlist($where, $pageIndex, $pageSize);

        return Basic::json($result);
    }

    /**
     *  员工详情
     */
    public function detail(Request $request)
    {
        $param['id'] = $request->param('id', null);

        /* 验证 */
        $this->validateData($param, 'Operator.detail');

        $Operator = new ServiceOperator();
        $result = $Operator->detail($param);
        return Basic::json($result);
    }

    /**
     *
     */
    public function getFieldEnum()
    {
        $params = $this->request->params;
        $Operator = new ServiceOperator();
        $result = $Operator->getFieldEnum($params['field'] ?? 'all');
        return Basic::json($result);
    }


    /**
     *  员工信息更新
     */
    public function save(Request $request)
    {
        $param['id'] = $request->param('id', null);
        $param['cname'] = $request->param('cname', null);
        $param['phone'] = $request->param('phone', null);
        $param['email'] = $request->param('email', null);
        $param['nickname'] = $request->param('nickname', null);
        $param['password'] = $request->param('password', null);
        $param['department'] = $request->param('department', null);
        $param['position'] = $request->param('position', null);
        $param['status'] = $request->param('status', null);
        $param['remark'] = $request->param('remark', null);
        $param['isadmin'] = $request->param('isadmin', null);
        $param['roles'] = $request->param('roles', null);
        $param['job'] = $request->param('job', null);

        /* 验证 */
        $this->validateData($param, 'Operator.add');

        $Operator = new ServiceOperator();
        $result = $Operator->save($param);
        return Basic::json($result);
    }

    /**
     *  员工分配角色
     */
    public function assignRole(Request $request)
    {
        $param['operator_id'] = $request->operator_id;
        $param['id'] = $request->param('id', null);
        $param['role_ids'] = $request->param('role_ids', null);

        /* 验证 */
        $this->validateData($param, 'Operator.assignRole');

        $Operator = new ServiceOperator();
        $result = $Operator->assignRole($param);
        return Basic::json($result);
    }


    /**
     *  移出员工
     */
    public function remove(Request $request)
    {
        $param['id'] = $request->param('id', null);

        /* 验证 */
        $this->validateData($param, 'Operator.remove');

        $Operator = new ServiceOperator();
        $result = $Operator->remove($param);
        return Basic::json($result);
    }

    /**
     * 删除员工
     */
    public function delete(Request $request)
    {
        $param['operator_id'] = $request->operator_id;
        $param['ids'] = $request->param('ids', null);

        /* 验证 */
        $this->validateData($param, 'Operator.del');

        $Operator = new ServiceOperator();
        $result = $Operator->del($param);
        return Basic::json($result);
    }

    public function getSelect(Request $request)
    {
        $Operator = new ServiceOperator();
        $result = $Operator->getSelect($request->operator_id);
        return Basic::json($result);
    }

    /**
     * @describe
     * @author: Bobo
     * @param [type] @required
     * @return Renderable
     */
    public function create()
    {

        $params = $this->request->params;
        $params['operator_id'] = $this->request->operator_id;

        /* 验证 */
        $this->validateData($params, 'Operator.create');

        $result = $this->service->create($params);
        return Basic::json($result);
    }
    /**
     * 更新员工
     * @describe 更新员工
     * @author: Bobo
     * @date: 2024-09-13 12:39:36
     * @param [int] $id 员工ID @required
     * @param [array] $data 更新员工的数据 @required
     * @return Renderable
     */
    public function update()
    {

        $params = $this->request->params;
        $params['operator_id'] = $this->request->operator_id;

        /* 验证 */
        $this->validateData($params, 'Operator.update');

        $result = $this->service->update($params);
        return Basic::json($result);
    }
}
